package com.erp.controller;

import com.erp.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author lqa
 * @Date 2022-04-20 15:56:07
 */
public class LoginServlet extends HttpServlet {
    UserService userService = new UserService();
    Logger logger = LoggerFactory.getLogger(LoginServlet.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;chartset=UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String verify = req.getParameter("verify");
        try {
            String getVerify =String.valueOf(req.getSession().getAttribute("codes"));
            if(!verify.equals(getVerify)){
                throw  new Exception("验证码错误！");
            }
            userService.login(username, password);
            logger.info("用户名：{},密码：{},验证码：{},结果：{}",
                    username,password,verify,"登录成功！");
            resp.getWriter().println("登录成功！");
        } catch (Exception e) {
            logger.error("用户名：{},密码：{},验证码：{},结果：{}-{}",
                    username,password,verify,"登录失败！",e.getMessage());
            resp.getWriter().println(e.getMessage());
        }

    }
}